package com.binguo.helper;

import com.binguo.qiniu.QiniuConstant;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.common.Zone;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.IOException;

/**
 * @author yuritian
 * @Title: UploadIMGHelper
 * @ProjectName binguo
 * @Description: TODO
 * @date 2019-05-1711:09
 */
@Component
public class UploadIMGHelper {



    //简单上传，使用默认策略，只需要设置上传的空间名就可以了
    public String getUpToken() {
        String upToken = QiniuConstant.auth.uploadToken(QiniuConstant.bucket);
        return upToken;
    }

    private static boolean upload(byte[] content, String key) throws Exception{
        //构造一个带指定Zone对象的配置类
        Configuration cfg = new Configuration(Zone.zone0());
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        //如果是Windows情况下，格式是 D:\\qiniu\\test.png
        System.out.println(key);

        String upToken = QiniuConstant.auth.uploadToken(QiniuConstant.bucket);
        try {
            Response response = uploadManager.put(content, key, upToken);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
            System.out.println(putRet.key);
            System.out.println(putRet.hash);

            return true;
        } catch (QiniuException ex) {
            Response r = ex.response;
            System.err.println(r.toString());
            try {
                System.err.println(r.bodyString());
            } catch (QiniuException ex2) {
                //ignore
            }

            return false;
        }
    }

    //上传图片调用
    public String upload(File uploadPictureFile, String pictureName) throws IOException {
        try {
            //创建上传对象
            Configuration cfg = new Configuration(Zone.zone0());
            //...其他参数参考类注释
            UploadManager uploadManager = new UploadManager(cfg);
            //调用put方法上传
            Response res = uploadManager.put(uploadPictureFile, pictureName, getUpToken());
            //打印返回的信息
            return res.bodyString();
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                //ignore
            }
            return null;
        }
    }


    public String delete(String pictureName) throws IOException {
        System.out.println(pictureName);
        try {
            Configuration cfg = new Configuration(Zone.zone0());
            BucketManager bucketManager = new BucketManager(QiniuConstant.auth, cfg);
            bucketManager.delete(QiniuConstant.bucket, pictureName);
        } catch (QiniuException ex) {
            //如果遇到异常，说明删除失败
            System.err.println(ex.code());
            System.err.println(ex.response.toString());
        }
        return null;
    }
}
